package zhoufu.sz.sanxiao_check.weiget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.Target;

import zhoufu.sz.sanxiao_check.R;

/**
 * Created by hyc on 16-7-7.
 * 项目 ： SanXiao_Check
 * 开发 ： hyc
 * 时间 ： 16-7-7 下午3:10
 * 功能 ： TODO
 */

public class ImageWithDelView extends FrameLayout {

    private int _sizeLimit;
    // TODO: 16-7-12 可以优化
    private final static int SIZE_BTN = 3;
    private Drawable _backDrawble;
    private ImageView _img;
    private Button _delBtn;
    private TextView _Tv_center;
    /**
     * 可能在大图片的时候需要进行处理
     */
    private static ViewGroup.LayoutParams _defaultParams;
    private static ViewGroup.LayoutParams _limitParams;


    public ImageView getImg() {
        return _img;
    }

    public ImageWithDelView(Context context) {
        this(context, null);
    }

    public ImageWithDelView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageWithDelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        final View inflate = LayoutInflater.from(context).inflate(R.layout.layout_img_del, this, true);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ImageWithDelView);
        _backDrawble = ta.getDrawable(R.styleable.ImageWithDelView_set_background);
        _sizeLimit = ta.getInteger(R.styleable.ImageWithDelView_size_limit, -1);
        ta.recycle();
        RelativeLayout la = (RelativeLayout) inflate.findViewById(R.id.img_del_container);
        _Tv_center = (TextView) inflate.findViewById(R.id.shopstore_tv);
        if (null == _backDrawble) {
//            _backDrawble=context.getResources().getDrawable(R.drawable.shape_white_bg_underline);
        } else {
            _Tv_center.setVisibility(GONE);
        }

//        la.setBackground(_backDrawble);
        _img = (ImageView) inflate.findViewById(R.id.idcard_top);
        _delBtn = (Button) inflate.findViewById(R.id.delete_idcard_top);

        inflate.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                inflate.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                ViewGroup.LayoutParams layoutParams = inflate.getLayoutParams();


                int height = layoutParams.height;
                int width = layoutParams.width;

                if (_sizeLimit > 0) {
                    if (_limitParams != null) {

                    }
                    _limitParams = _delBtn.getLayoutParams();
                    _limitParams.height = height / _sizeLimit;
                    _limitParams.width = width / _sizeLimit;
                } else {
                    _defaultParams = _delBtn.getLayoutParams();
                }


            }
        });

        _img.setBackground(_backDrawble);
        _img.setOnClickListener(_imgClickListener);
        _delBtn.setVisibility(GONE);
        _delBtn.setOnClickListener(_btnClickListener);

    }

    private OnClickListener _imgClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (_imageOnclickListener == null) {
                throw new NullPointerException("必须调用 #setImageOnclickListener()！");
            }
            _imageOnclickListener.imageClick(ImageWithDelView.this);
        }
    };
    ImageOnclickListener _imageOnclickListener;

    public ImageOnclickListener getImageOnclickListener() {
        return _imageOnclickListener;
    }

    public void setImageOnclickListener(ImageOnclickListener imageOnclickListener) {
        _imageOnclickListener = imageOnclickListener;
    }

    public void clearPic() {
        Drawable drawable = _img.getDrawable();
        if (drawable != null) {
            _img.setImageDrawable(null);
        }
        _delBtn.setVisibility(GONE);
        if (_backDrawble == null) {
            _Tv_center.setVisibility(VISIBLE);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (_delBtn != null) {
            if (_sizeLimit > 0 && _limitParams != null)
                _delBtn.setLayoutParams(_limitParams);
            else if (_sizeLimit < 0 && _defaultParams != null) {
                _delBtn.setLayoutParams(_defaultParams);
            }
        }
    }

    /**
     * 监听图片的点击和删除事件
     */
    public  interface ImageOnclickListener {
        void imageClick(ImageWithDelView view);

        void imageDelClick(ImageWithDelView onClickListener);
    }

    public void setImageSource(String source) {
        if (TextUtils.isEmpty(source))
            return;
        Glide.get(_img.getContext());
        Glide.with(_img.getContext()).load(source).asBitmap().centerCrop().into(_img);
        _delBtn.setVisibility(VISIBLE);
        _Tv_center.setVisibility(GONE);
    }

    private OnClickListener _btnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            clearPic();
            _imageOnclickListener.imageDelClick(ImageWithDelView.this);
        }
    };

    public boolean hasDrawWithImg() {
        return _img.getDrawable() != null;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        setImageOnclickListener(null);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ImageWithDelView that = (ImageWithDelView) o;

        if (_img != null ? !_img.equals(that._img) : that._img != null) return false;
        if (_delBtn != null ? !_delBtn.equals(that._delBtn) : that._delBtn != null) return false;
        return _Tv_center != null ? _Tv_center.equals(that._Tv_center) : that._Tv_center == null;

    }

    @Override
    public int hashCode() {
        int result = _img != null ? _img.hashCode() : 0;
        result = 31 * result + (_delBtn != null ? _delBtn.hashCode() : 0);
        result = 31 * result + (_Tv_center != null ? _Tv_center.hashCode() : 0);
        return result;
    }

}
